import { defineStore } from 'pinia';
import { getMenuList } from '@/api/menu';
import { generateRoutes } from '@/router';
import { ElMessage } from "element-plus";

export const routerStore = defineStore('routerStore', {
	state: () => ({
		menuRoutes: [],
		// searchMenu: [],
		// routes: []
	}),
	actions: {
		async getMenuRoutes() {
			const res = await getMenuList();
			if (res.success) {
				const routes = generateRoutes(res.data);
				this.menuRoutes = [];
				this.menuRoutes.push(...routes);
				return this.menuRoutes;
			} else {
				ElMessage.error(res.msg || '菜单错误，请重试');
			}
		},
		setSearchMenu(routers) {
			this.searchMenu = routers;
		},
		setRoutes(routers) {
			this.routes = routers;
		}
	}
})
